package com.czl.module_work.viewModel

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.MembersBean
import com.czl.base.data.bean.TakeCareDispatchBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import java.util.regex.Matcher
import java.util.regex.Pattern


/**
 * 创建日期：2021/12/28  15:27
 * 类说明:
 * @author：86152
 */
class MembersSelectViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var orderTypeId = ""

    var filterParams = ObservableField<String>()

    var allMembers = mutableListOf<MembersBean>()

    var filterMembers = mutableListOf<MembersBean>()

    val cancelClick = BindingCommand<Void>(BindingAction {
        filterParams.set("")
        uc.loadRoomEvent.postValue(allMembers)
    })
    val search = BindingCommand<String> { t ->
        filterParams.set(t ?: "")
        val list = search(name = filterParams.get(), allMembers)
        filterMembers.clear()
        filterMembers.addAll(list)
        uc.loadRoomEvent.postValue(filterMembers)
        if (TextUtils.isEmpty(filterParams.get())) {
            uc.loadRoomEvent.postValue(allMembers)
        }
    }


    val uc = UiChangeEvent()

    class UiChangeEvent {
        val loadRoomEvent: SingleLiveEvent<List<MembersBean>> = SingleLiveEvent()
    }

    fun search(name: String?, list: List<MembersBean>): List<MembersBean> {
        val results: MutableList<MembersBean> = arrayListOf()
        val pattern = Pattern.compile(name)
        for (i in list.indices) {
            val matcher = pattern.matcher(list[i].nickName)
            if (matcher.find()) {
                results.add(list[i])
            }
        }
        return results
    }

    fun setOderTypeId(id: String) {
        this.orderTypeId = id
    }

    fun getUserRooms(isHandlerUser: Boolean) {
        val req = if (isHandlerUser) {
            model.getHandleUser(orderTypeId)
        } else {
            model.listUser(model.getAreaId())
        }
        req.compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<MembersBean>>>() {
                override fun onResult(t: BaseBean<List<MembersBean>>) {
                    if (t.data == null) return
                    if (t.code == 200) {
                        allMembers.clear()
                        allMembers.addAll(t.data ?: listOf())
                        uc.loadRoomEvent.postValue(t.data)
                    } else {
                        uc.loadRoomEvent.postValue(null)
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    uc.loadRoomEvent.postValue(null)
                    showErrorToast(msg)
                }
            })
    }

}